package com.focusai.efairy.model.request;

import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.model.response.DeviceDetailInfoResponse;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.ParseException;
import com.focusai.efairy.network.request.base.Request;
import com.focusai.efairy.network.utils.UrlUtils;
import com.focusai.efairy.utils.Base64Utils;
import com.focusai.efairy.utils.GsonHelper;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by luo_guangwen on 2017/8/12.
 */

public class GetDeviceDetailRequest extends Request<DeviceDetailInfoResponse> {
    private final static String TAG = GetDeviceDetailRequest.class.getName();
    private long deviceId;   //设备编号
    private String startData; //格式：YYYY-MM-dd (2017-07-14)，默认传当天时间
    private int days; //设备过去多少天的数据，默认7天

    public GetDeviceDetailRequest(long deviceId, String startData, int days, Response.Listener<DeviceDetailInfoResponse> listener) {
        super(Method.GET, UrlUtils.createUrl("/appapi/device"), listener);
        this.deviceId = deviceId;
        this.startData = startData;
        this.days = days;
    }

    @Override
    public Map<String, String> getParams() throws JSONException {
        addParam("efairyuser_id", CacheManager.getUserId());//用户编号
        addParam("access_token", CacheManager.getAccessToken());
        addParam("efairydevice_id", String.valueOf(deviceId));
        addParam("start_time", startData);
        addParam("days", String.valueOf(days));
        return super.getParams();
    }

    @Override
    protected DeviceDetailInfoResponse parse(String result) throws ParseException {
        DeviceDetailInfoResponse deviceDetailInfoResponse = GsonHelper.getInstance().fromJson(result, new TypeToken<DeviceDetailInfoResponse>() {
        }.getType());
        if (deviceDetailInfoResponse != null && deviceDetailInfoResponse.data_stream_list != null) {
            String data;
            deviceDetailInfoResponse.deviceReltimeDataList = new ArrayList<>();
            for (String str : deviceDetailInfoResponse.data_stream_list) {
                data = Base64Utils.decompressForGzip(str);
                List<DeviceDetailInfoResponse.RealtimeData> list = new ArrayList<>();
                try {
                    JSONArray jsonArray = new JSONArray(data);
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            if (i % 5 == 0) {
                                DeviceDetailInfoResponse.RealtimeData realtimeData = GsonHelper.getInstance().fromJson(jsonArray.get(i).toString(), DeviceDetailInfoResponse.RealtimeData.class);
                                list.add(realtimeData);
                            }
                        }
                    }
                    deviceDetailInfoResponse.deviceReltimeDataList.add(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            deviceDetailInfoResponse.data_stream_list.clear();
            deviceDetailInfoResponse.data_stream_list = null;
        }
        return deviceDetailInfoResponse;
    }

}
